package com.flyee.controller{
	
	import com.flyee.controller.timer.MasterCooldownTimer;
	import com.flyee.controller.tools.cartoon.ui.showequip.ShowEquipWindow;
	import com.flyee.service.CharactorService;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.KeyboardEventManager;
	import com.flyee.utils.MyLog;
	import com.flyee.utils.Obsevable;
	import com.flyee.utils.Obsever;
	import com.flyee.viewer.PackagePanelViewer;
	import com.flyee.viewer.SkillButtonViewer;
	import com.flyee.viewer.SkillPanelViewer;
	import com.flyee.viewer.TargetWindowViewer;
	import com.flyee.viewer.pack.helper.ItemDataModel;
	
	import flash.events.MouseEvent;
	
	import mx.events.CollectionEvent;

	public class SkillPanelController extends Obsevable implements Obsever{
		
		[Inject]
		public var charactorService : CharactorService;
		
		public function SkillPanelController(){
		}

		public function execute(o:Obsevable, command:String=null, args:*=null) : void {
			switch(command){
				case "updateHP" : 
					updateHP(args[0]);
					break ;
				case "updateLevel":
					updateLevel(args[0]);
					break;
				case "updateExp":
					updateExp(args[0],args[1]);
					break;
				default :
					break;
			}
		}
		
		public function init() : void {
			var panel : SkillPanelViewer = new SkillPanelViewer();
			ObjectCollection.skillPanelViewer = panel;
			ObjectCollection.uiContainer.addChild(panel);
			panel.x = 300;
			panel.y = 447;
			panel.setHP(ObjectCollection.master.hp);
			panel.setMP(ObjectCollection.master.mp);
			
			var targetWindow : TargetWindowViewer = new TargetWindowViewer();
			ObjectCollection.targetWindowViewer = targetWindow;
			ObjectCollection.uiContainer.addChild(targetWindow);
			targetWindow.visible = false;
			targetWindow.x = 350;
			initListener(panel);
			KeyboardEventManager.getInstance().registerListener(66,openPackPanel);
			KeyboardEventManager.getInstance().registerListener(67,openMasterEquip);
			//KeyboardEventManager.getInstance().registerListener(71,openPickupPanel);

			//设置主角技能冷却时间
			var cdTimer : MasterCooldownTimer = new MasterCooldownTimer(ObjectCollection.master);
			ObjectCollection.masterCDTimer = cdTimer;
		}
		
		private function openMasterEquip(data:Object):void{
			var equipViewer:ShowEquipWindow = ObjectCollection.equipViewer;
			if(equipViewer){
				if(ObjectCollection.uiContainer.contains(equipViewer)){
					ObjectCollection.uiContainer.removeChild(equipViewer);
				}
				equipViewer = null;
				ObjectCollection.equipViewer = null;
			}else{
				equipViewer = new ShowEquipWindow();
				ObjectCollection.uiContainer.addChild(equipViewer);
				ObjectCollection.equipViewer = equipViewer;
			}
		}
		
		/* private function openPickupPanel(data:Object):void{
			var viewer:PickUpItemViewer = new PickUpItemViewer();
			ObjectCollection.uiContainer.addChild(viewer);
		} */
		
		private function openPackPanel(data:Object):void{
			var packViewer:PackagePanelViewer = ObjectCollection.packPanelViewer;
			if(packViewer){
				packViewer.dispose();
				if(ObjectCollection.uiContainer.contains(packViewer)){
					ObjectCollection.uiContainer.removeChild(packViewer);
				}
				packViewer = null;
				ObjectCollection.packPanelViewer = null;
			}else{
				packViewer = new PackagePanelViewer();
				ObjectCollection.uiContainer.addChild(packViewer);
				ObjectCollection.packPanelViewer = packViewer;
			}
		}
		
		public function initListener(panel : SkillPanelViewer) : void {
			panel.skillBtn1.addEventListener(MouseEvent.CLICK, skillClick);
			panel.skillBtn2.addEventListener(MouseEvent.CLICK, skillClick);
			panel.skillBtn3.addEventListener(MouseEvent.CLICK, skillClick);
			panel.skillBtn4.addEventListener(MouseEvent.CLICK, skillClick);
			panel.skillBtn5.addEventListener(MouseEvent.CLICK, skillClick);
			panel.drugItems.addEventListener(CollectionEvent.COLLECTION_CHANGE,dataChange);
		}
		
		private function dataChange(event:CollectionEvent):void{
			MyLog.debug("update data!!!!");
			var dataMode:ItemDataModel = ObjectCollection.skillPanelViewer.drugItems.getItemAt(0) as ItemDataModel;
			var label:String = dataMode.action;
			ObjectCollection.skillPanelViewer.skillBtn5.count = dataMode.currentCapability;
			if(ObjectCollection.packPanelViewer){
				
			}
			/* switch(label){
				case "init":
					break;
				case "add":
					break;
				case "remove":
					ObjectCollection.skillPanelViewer.skillBtn5.count = dataMode.currentCapability;
					break;
				default:
					break;
			} */
		}
		
		/* public function itemClick(event:MouseEvent):void{
			var button : SkillButtonViewer = event.currentTarget as SkillButtonViewer;
			var skillController:SkillPanelController = 
					ObjectCollection.beansContext.getObject("skillPanelController") as SkillPanelController;
			skillController.charactorService.sendSkill(4);
		} */
		
		public function skillClick(event : MouseEvent) : void {
			var button : SkillButtonViewer = event.currentTarget as SkillButtonViewer;
			charactorService.sendSkill(button.skillId);
		}
		
		public function changeEquip(equipId:String):void{
			charactorService.sendChangeEquip(equipId);
		}
		
		public function updateHP(hp : int):void{
			ObjectCollection.skillPanelViewer.setHP(hp);
			if(ObjectCollection.master.target == ObjectCollection.master){
				ObjectCollection.targetWindowViewer.hp = hp;
			}
		}
		
		public function updateExp(exp:int,nextexp:int):void{
			ObjectCollection.skillPanelViewer.setEXP(exp,nextexp);
		}
		
		public function updateLevel(lvl:int):void{
			ObjectCollection.skillPanelViewer.setGrade(lvl);
		}
		
		public function updateMP(mp : int):void{
			ObjectCollection.skillPanelViewer.setMP(mp);
		}
	}
}